// pages/ businessDetails/ businessDetails.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isShowDialog: false,
    discount: 100,
    specSelected: {
      idx: 0,
      id: 0
    },
    cycleSelected: {
      idx: 0,
      id: 0
    },
    shareAppMessageTitle: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      id: options.id,
      discount: app.globalData.userInfo.discount ? parseFloat(app.globalData.userInfo.discount) : 100
    })
    this.getGoodsDetail();
  },

  // 获取商品详情
  getGoodsDetail: function() {
    var that = this;
    var data = {
      point_url: app.server.agentApi.goodsDetail,
      openid: app.globalData.openid,
      id: this.data.id
    };
    app.server.getConnect(data).then(function(res) {
      let temp = res.data;
      temp.body = temp.body.replace(/\<img/gi, '<img style="max-width:100%;height:auto" ');
      temp.pic_list = temp.pic_list.split(',');
      that.setData({
        info: temp,
        shareAppMessageTitle: temp.goods_name
      })
      wx.setNavigationBarTitle({
        title:temp.goods_name
      })
    })
  },

  // 跳转业务首页
  toBusiness: function() {
    // wx.redirectTo({
    //   url: '/pages/agent/plugins/business/index/index'
    // })
    wx.navigateBack();
  },

  // 选择规格一
  selectSpec: function(e) {
    var selected = {
      idx: e.currentTarget.dataset.idx,
      id: e.currentTarget.dataset.id
    }
    var cycleSelected = {
      idx: 0,
      id: 0
    }
    this.setData({
      specSelected: selected,
      cycleSelected: cycleSelected
    })
  },

  // 选择规格二
  selectCycle: function(e) {
    var selected = {
      idx: e.currentTarget.dataset.idx,
      id: e.currentTarget.dataset.id
    }
    this.setData({
      cycleSelected: selected
    })
  },

  // 立即购买
  buyNow: function() {
    var that = this;
    var isShowDialog = this.data.isShowDialog;
    this.setData({
      isShowDialog: !isShowDialog
    })
    if (isShowDialog) {
      app.showConfirm('付款方式', '', true, function() {
        that.setData({
          pay_type: 1
        })
        that.toPay();
      }, function() {
        that.setData({
          pay_type: 0
        })
        that.toPay();
      }, '余额', '赠送金')
    }
  },

  // 支付
  toPay: function() {
    var that = this;
    var goods_version = this.data.specSelected.idx + ',' + this.data.cycleSelected.idx;
    var data = {
      point_url: app.server.agentApi.submitOrder,
      openid: app.globalData.openid,
      goods_id: this.data.id,
      amount: 1,
      goods_type_name: this.data.info.goods_version[this.data.specSelected.idx].goods_version,
      // goods_version: JSON.stringify(this.data.info.goods_version).toString(),
      goods_version: goods_version,
      pay_type: this.data.pay_type,
      remarks: ''
    };
    app.server.getConnect(data).then(function(res) {
      console.log(res);
      app.showConfirm('提示', '支付成功!是否跳转订单页', true, function() {
        wx.navigateTo({
          url: '../order/order?otype=1',
        })
      })
    }, function(res) {
      console.log(res);
      wx.hideLoading();
      app.showConfirm('提示', res.msg, false, function() {
        if (res.status === 206) {
          wx.navigateTo({
            url: '../order/order?otype=0',
          })
        }
      });
    })
  },

  showDialog: function() {
    this.setData({
      isShowDialog: true
    })
  },

  hiddenDialog: function() {
    this.setData({
      isShowDialog: false
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    app.pageOnloadInit(this, app);
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    app.pageOnloadInit(this, app);
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
  
  }
})
